package leetcode.editor.cn;

//Java：移除链表元素
public class RemoveLinkedListElements {
    public static void main(String[] args) {
        Solution solution = new RemoveLinkedListElements().new Solution();
        // TO TEST
    }
    //leetcode submit region begin(Prohibit modification and deletion)

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     * int val;
     * ListNode next;
     * ListNode() {}
     * ListNode(int val) { this.val = val; }
     * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
     * }
     */

    class Solution {
        public ListNode removeElements(ListNode head, int val) {
            if (head == null) {
                return head;
            }
            ListNode fastNode = head;
            ListNode result = new ListNode(-1, head);
            ListNode slowNode = result;
            while (fastNode != null) {
                if (fastNode.val != val) {
                    slowNode = fastNode;
                } else {
                    slowNode.next = fastNode.next;
                }
                fastNode = fastNode.next;
            }
            return result.next;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}